#!/usr/bin/python
#
# Copyright 2007 DeWitt Clinton, All Rights Reserved

"""Entities for OpenID URL relationships."""

__author__ = 'dewitt@unto.net'

from django.db import models

class Relationship(models.Model):
  """A single identity to identity relationship."""

  
  RELATIONSHIP_TYPES = (
    ('contact', 'Someone you know how to get in touch with. Often symmetric.'),
    ('acquaintance', 'Someone who you have exchanged greetings and not much more (if any) maybe a short conversation or two. Often symmetric.'),
    ('met', 'Someone who you have actually met in person. Symmetric.'),
    ('co-worker', 'Someone a person works with, or works at the same organization as. Symmetric. Usually transitive.'),
    ('colleague', 'Someone in the same field of study/activity. Symmetric. Often transitive.'),
    ('co-resident', 'Someone you share a street address with. Symmetric and transitive.'),
    ('neighbor', 'Someone who lives nearby, perhaps only at an adjacent street address or doorway. Symmetric. Often transitive.'),
    ('child', """A person's genetic offspring, or someone that a person has adopted and takes care of. Inverse is parent."""),
    ('parent', 'Inverse of child.'),
    ('sibling', 'Someone a person shares a parent with. Symmetric. Usually transitive.'),
    ('spouse', 'Someone you are married to. Symmetric. Not transitive.'),
    ('kin', 'A relative, someone you consider part of your extended family. Symmetric and typically transitive.'),
    ('muse', 'Someone who brings you inspiration. No inverse.'),
    ('crush', 'Someone you have a crush on. No inverse.'),
    ('date', 'Someone you are dating. Symmetric. Not transitive.'),
    ('sweetheart', 'Someone with whom you are intimate and at least somewhat committed, typically exclusively. Symmetric. Not transitive.'),
    ('me', 'A link to yourself at a different URL. Exclusive of all other XFN values. Required symmetric. There is an implicit "me" relation from a subdirectory to all of its contents.'),
    ('friend', 'Someone you are a friend to. A compatriot, buddy, home(boy|girl) that you know. Often symmetric.'),
  )

  # """The identity asserting the relationship""",
  from_identity = models.URLField("""The OpenID URL of the identity asserting this relationship."""
                                  maxlength=1024)

  # """The identity receiving the relationship"""
  to_identity = models.URLField("""The OpenID URL of the identity receiving this relationship."""
                                maxlength=1024)

  type = models.CharField("""A normarlized representation of the relationship type.""",
                          maxlength=1024)
  
  def __unicode__(self):
    """Return this instance as an Unicode string."""
    return "%s -> %s (%s)" % (self.from_identity, self.to_identity, self.type)

  def __str__(self):
    """Return this instance as a string."""
    # TODO(dewitt): This seems wrong, but it works
    return self.__unicode__()

  class Admin:
    """Allow this model to be managed by the admin console."""
    pass

  class Meta:
    unique_together = (("from_identity", "to_identity"),)


class Event(models.Model):
  """A single event in the event queue."""
  
  TYPE_CHOICES = (
    ('crawl', 'Crawl and parse the value'),
  )

  type = models.CharField(maxlength=64, choices=TYPE_CHOICES)

  STATUS_CHOICES = (
    ('pending', 'The event has yet to be processed.'),
    ('in-progress', 'The event is currently being processed.'),
    ('completed', 'The event is finished being processed.'),
    ('error', 'The event encountered an error when being processed.'),
  )

  status = models.CharField(maxlength=64,
                            choices=STATUS_CHOICES,
                            default='pending')
  
  value = models.CharField(maxlength=1024)
  
  created_date = models.DateTimeField(auto_now_add=True)

  modified_date = models.DateTimeField(auto_now=True)

  def __unicode__(self):
    """Return this instance as an Unicode string."""
    return "%s %s (%s)" % (self.type, self.value, self.status)

  def __str__(self):
    """Return this instance as a string."""
    # TODO(dewitt): This seems wrong, but it works
    return self.__unicode__()

  class Admin:
    """Allow this model to be managed by the admin console."""
    pass


  
